package com.gzhryc.shared_device.oem.code.config.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.IDTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.config.dao.APISecurityConfigDao;
import com.gzhryc.shared_device.oem.code.config.dao.db.APISecurityConfig;
import com.gzhryc.shared_device.oem.code.config.services.dto.APISecurityConfigSearch;

public class APISecurityConfigService {

	static Logger log = Logger.getLogger(APISecurityConfigService.class);

    public static APISecurityConfigService self() {
        return MultiDBTools.getService(APISecurityConfigService.class, true);
    }

    public static APISecurityConfigService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, APISecurityConfigService.class, true);
    }

    APISecurityConfigDao dao;

    public APISecurityConfigService(String jdbcKey) {
        this.dao = new APISecurityConfigDao(jdbcKey);
    }
    
    public List<APISecurityConfig> findBySearch(APISecurityConfigSearch search,Integer pageNo,Integer pageLimit){
    	Conditions condition = dao.buildConditions(search);
    	try {
			return dao.findByCondition(condition, pageNo, pageLimit);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return null;
    }
    
    public Long countBySearch(APISecurityConfigSearch search) {
    	Conditions condition = dao.buildConditions(search);
    	try {
			return dao.count(condition);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return 0L;
    }
    
    public APISecurityConfig getById(Long id) {
    	try {
			return dao.getById(id);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return null;
    }
    
    public boolean add(APISecurityConfig apiSecurityConfig) throws LogicException{
    	if(StringTools.isNotBlank(apiSecurityConfig.getToken())) {
    		apiSecurityConfig.setToken(IDTools.getUUID());
    	}
    	apiSecurityConfig.setCreateDate(new Date());
    	try {
			return this.dao.insert(apiSecurityConfig) > 0;
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return false;
    }
    
    public boolean updateNotNull(APISecurityConfig apiSecurityConfig) throws LogicException {
    	try {
			return this.dao.updateNotNull(apiSecurityConfig) > 0;
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return false;
    }
    
    public boolean deleteById(Long id) throws LogicException{
    	try {
			return this.dao.deleteById(id) > 0;
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return false;
    }
}
